package com.feihong.yw189.activity.yonghuzhongxin.wodexiaoxi;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.base.BaseActivity;
import com.feihong.yw189.activity.kaika.moneyout.BalanceTurnOutActivity;
import com.feihong.yw189.adapter.ShareListDetailAdapter;
import com.feihong.yw189.app.App;
import com.feihong.yw189.global.RequestTools;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.message.LoginMessage;
import com.feihong.yw189.model.User.money.ShareListDetailModel;
import com.holenzhou.pullrecyclerview.PullRecyclerView;
import com.holenzhou.pullrecyclerview.layoutmanager.XLinearLayoutManager;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/4/13 0013.
 */

public class ShouYiActivity extends BaseActivity {

    @BindView(R.id.top_back_layout)
    RelativeLayout layout;
    @BindView(R.id.iv_lin)
    ImageView ivLink;
    @BindView(R.id.rl_goback)
    RelativeLayout rl_goBack;
    @BindView(R.id.txt_pagetitle)
    TextView textTitle;
    @BindView(R.id.iv_back)
    ImageView ivWriteBack;
    @BindView(R.id.pull_recycler_view)
    PullRecyclerView mPullRecyclerView;
    @BindView(R.id.tvYesterday)
    TextView tvYestertady;
    @BindView(R.id.tvTerday)
    TextView tvTerday;
    @BindView(R.id.tvTerdayPai)
    TextView tvTerdayPai;
    List<ShareListDetailModel.DataBean.ListBean> mDataList;
    ShareListDetailAdapter mAdapter;
    @BindView(R.id.iv_confirm)
    TextView ivConfirm;
    @BindView(R.id.tv_backmoney_num) //返利金额
    TextView tvBackmoneyNum;

    @BindView(R.id.share_tixian)
    ImageView shareTixian;
    @BindView(R.id.share_suss_user)
    TextView shareSussUser;
    @BindView(R.id.share_fail_user)
    TextView shareFailUser;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refreshLayout;

    private int pageSize = 10;
    private int pageGlobal = 2;

    private Unbinder unbinder;
    private ShareListDetailModel.DataBean.ListBean model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouyi);
        unbinder = ButterKnife.bind(this);

        initView();
    }

    @Override
    public void setTheme() {

    }


    private void initView() {
        ivLink.setVisibility(View.GONE);
        setPageTitle("预约明细");
        bindData();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                initView();
            }
        });
    }

    private void bindData() {
        mDataList = new ArrayList<>();
        // 初始化PullRecyclerView
        mPullRecyclerView.setLayoutManager(new XLinearLayoutManager(this));
        mAdapter = new ShareListDetailAdapter(this, R.layout.share_list_detail, mDataList);
        mPullRecyclerView.setAdapter(mAdapter);

        mPullRecyclerView.setColorSchemeResources(R.color.appBlue); // 设置下拉刷新的旋转圆圈的颜色
        mPullRecyclerView.enablePullRefresh(false); // 开启下拉刷新，默认即为true，可不用设置
        mPullRecyclerView.enableLoadMore(true);

        mPullRecyclerView.enableLoadDoneTip(false, R.string.load_done_tip); // 开启数据全部加载完成时的底部提示，默认为false
        //刷新监听
        mPullRecyclerView.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {
            @Override
            public void onPullRefresh() {
                // 下拉刷新网络请求
                Callback.Cancelable doawCallBack = App.getApp().getHttpTools().shareDetaiList("1", "10", CallBackDown);
                RequestTools.addNewRequest(SysteMessageListActivity.class.getName(), doawCallBack);
            }

            @Override
            public void onLoadMore() {
                Callback.Cancelable downCallBack = App.getApp().getHttpTools().shareDetaiList("" + pageGlobal, "10", callBackUp);
                RequestTools.addNewRequest(SysteMessageListActivity.class.getName(), downCallBack);
            }
        });
        mPullRecyclerView.postRefreshing();
        mPullRecyclerView.stopLoadMore();
    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    @OnClick({R.id.rl_goback, R.id.share_tixian})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_goback:
                finish();
                break;
            case R.id.share_tixian:
                startActivity(new Intent(ShouYiActivity.this, BalanceTurnOutActivity.class));
                break;
            default:
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    //下拉获取信息
    RequestCallBack<String> CallBackDown = new RequestCallBack<String>() {
        @Override
        public void onSuccess(String result) {
//            result = "{\"status\":1,\"info\":\"请求成功！\",\"data\":{\"token\":\"99843a1872278767437bd28d54392835\",\"tongji\":{\"zongji\":\"0\",\"chenggong\":\"0\",\"shibai\":\"0\",\"fanli\":0},\"list\":[{\"order_sn\":\"2018032852571011\",\"mobile\":\"185****8088\",\"status\":\"2\",\"money\":\"0.00\",\"add_time\":\"2018-03-30\",\"ok_time\":\"\",\"status_name\":\"已验证\"},{\"order_sn\":\"2018032810155531\",\"mobile\":\"186****5393\",\"status\":\"1\",\"money\":\"50.00\",\"add_time\":\"2018-03-28\",\"ok_time\":\"\",\"status_name\":\"已提交\"},{\"order_sn\":\"2018032810049979\",\"mobile\":\"185****9878\",\"status\":\"5\",\"money\":\"0.00\",\"add_time\":\"2018-03-28\",\"ok_time\":\"\",\"status_name\":\"已取消\"},{\"order_sn\":\"2018032857545155\",\"mobile\":\"181****3888\",\"status\":\"2\",\"money\":\"0.00\",\"add_time\":\"2018-03-28\",\"ok_time\":\"\",\"status_name\":\"已验证\"},{\"order_sn\":\"2018032850100554\",\"mobile\":\"132****1500\",\"status\":\"1\",\"money\":\"0.00\",\"add_time\":\"2018-03-28\",\"ok_time\":\"\",\"status_name\":\"已提交\"},{\"order_sn\":\"2018032910210248\",\"mobile\":\"186****8164\",\"status\":\"2\",\"money\":\"5.00\",\"add_time\":\"2018-03-29\",\"ok_time\":\"\",\"status_name\":\"已验证\"},{\"order_sn\":\"2018032957505751\",\"mobile\":\"132****4321\",\"status\":\"1\",\"money\":\"0.00\",\"add_time\":\"2018-03-29\",\"ok_time\":\"\",\"status_name\":\"已提交\"},{\"order_sn\":\"2018032949565550\",\"mobile\":\"132****0366\",\"status\":\"1\",\"money\":\"0.00\",\"add_time\":\"2018-03-29\",\"ok_time\":\"\",\"status_name\":\"已提交\"},{\"order_sn\":\"2018032952565555\",\"mobile\":\"152****2673\",\"status\":\"4\",\"money\":\"5.50\",\"add_time\":\"2018-03-29\",\"ok_time\":\"\",\"status_name\":\"已完成\"},{\"order_sn\":\"2018033098544857\",\"mobile\":\"153****7321\",\"status\":\"5\",\"money\":\"0.00\",\"add_time\":\"2018-03-30\",\"ok_time\":\"\",\"status_name\":\"已取消\"}]},\"version\":\"Controller\"}";
            try {
                //json解析
                ShareListDetailModel shareModel = JSON.parseObject("" + result, ShareListDetailModel.class);
                //有数据
                if (shareModel != null) {
                    if (shareModel.getStatus() == 1) {
                        //返利金额
                        tvBackmoneyNum.setText(shareModel.getData().getTongji().getFanli());
                        //总计
                        tvYestertady.setText(shareModel.getData().getTongji().getZongji());
                        //成功
                        tvTerday.setText(shareModel.getData().getTongji().getChenggong());
                        //失败
                        tvTerdayPai.setText(shareModel.getData().getTongji().getShibai());


                        if (shareModel.getData().getList().size() > 0) {
                            //清除原数据,加载新数据,上拉加载
                            ShareListDetailModel.DataBean.ListBean model;
                            mDataList.clear();
                            //重置
                            pageGlobal = 2;
                            for (ShareListDetailModel.DataBean.ListBean message : shareModel.getData().getList()) {
                                model = new ShareListDetailModel.DataBean.ListBean();
                                model.setOrder_sn(message.getOrder_sn());
                                model.setStatus_name(message.getStatus_name());
                                model.setMobile(message.getMobile());
                                model.setAdd_time(message.getAdd_time());
                                mDataList.add(model);
                            }
                            mAdapter.notifyDataSetChanged();
                            //开启上拉加载更多
                            mPullRecyclerView.enableLoadMore(true);
                        } else {
                            //数据为空,显示空页面
                            mPullRecyclerView.addHeaderView(R.layout.my_null_data_layout);
                            mPullRecyclerView.setEmptyView(R.layout.my_null_data_layout);
                        }
                    } else {
                        //为空显示空页面
                        if (mDataList != null && mDataList.isEmpty()) {
                            mPullRecyclerView.addHeaderView(R.layout.my_null_data_layout);
                            mPullRecyclerView.setEmptyView(R.layout.my_null_data_layout);

                            View view = View.inflate(getApplication(), R.layout.my_null_data_layout, null);
                            TextView txt = (TextView) view.findViewById(R.id.null_data_layout_txt);

                            txt.setText("暂无记录");
                        } else {
                            showToast(shareModel.getInfo());
                        }
                    }
                } else {
                    showErrorToast("解析出错,请重试!");
                }
            } catch (Exception ex) {
            }
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            showToast(getResources().getString(R.string.connect_sever_error_toast) );
        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {
        }

        @Override
        public void onFinished() {
            mPullRecyclerView.stopRefresh();
            if (refreshLayout != null) {
                refreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.setRefreshing(false);
                    }
                }, 300);
            }
        }
    };

    //上拉更新信息
    RequestCallBack<String> callBackUp = new RequestCallBack<String>() {
        @Override
        public void onSuccess(String result) {
//            result = "{\"status\":1,\"info\":\"请求成功！\",\"data\":{\"token\":\"99843a1872278767437bd28d54392835\",\"tongji\":{\"zongji\":\"0\",\"chenggong\":\"0\",\"shibai\":\"0\",\"fanli\":0},\"list\":[{\"order_sn\":\"2018032852571011\",\"mobile\":\"185****8088\",\"status\":\"2\",\"money\":\"0.00\",\"add_time\":\"2018-03-30\",\"ok_time\":\"\",\"status_name\":\"已验证\"},{\"order_sn\":\"2018032810155531\",\"mobile\":\"186****5393\",\"status\":\"1\",\"money\":\"50.00\",\"add_time\":\"2018-03-28\",\"ok_time\":\"\",\"status_name\":\"已提交\"},{\"order_sn\":\"2018032810049979\",\"mobile\":\"185****9878\",\"status\":\"5\",\"money\":\"0.00\",\"add_time\":\"2018-03-28\",\"ok_time\":\"\",\"status_name\":\"已取消\"},{\"order_sn\":\"2018032857545155\",\"mobile\":\"181****3888\",\"status\":\"2\",\"money\":\"0.00\",\"add_time\":\"2018-03-28\",\"ok_time\":\"\",\"status_name\":\"已验证\"},{\"order_sn\":\"2018032850100554\",\"mobile\":\"132****1500\",\"status\":\"1\",\"money\":\"0.00\",\"add_time\":\"2018-03-28\",\"ok_time\":\"\",\"status_name\":\"已提交\"},{\"order_sn\":\"2018032910210248\",\"mobile\":\"186****8164\",\"status\":\"2\",\"money\":\"5.00\",\"add_time\":\"2018-03-29\",\"ok_time\":\"\",\"status_name\":\"已验证\"},{\"order_sn\":\"2018032957505751\",\"mobile\":\"132****4321\",\"status\":\"1\",\"money\":\"0.00\",\"add_time\":\"2018-03-29\",\"ok_time\":\"\",\"status_name\":\"已提交\"},{\"order_sn\":\"2018032949565550\",\"mobile\":\"132****0366\",\"status\":\"1\",\"money\":\"0.00\",\"add_time\":\"2018-03-29\",\"ok_time\":\"\",\"status_name\":\"已提交\"},{\"order_sn\":\"2018032952565555\",\"mobile\":\"152****2673\",\"status\":\"4\",\"money\":\"5.50\",\"add_time\":\"2018-03-29\",\"ok_time\":\"\",\"status_name\":\"已完成\"},{\"order_sn\":\"2018033098544857\",\"mobile\":\"153****7321\",\"status\":\"5\",\"money\":\"0.00\",\"add_time\":\"2018-03-30\",\"ok_time\":\"\",\"status_name\":\"已取消\"}]},\"version\":\"Controller\"}";

            try {
                //json解析
                ShareListDetailModel shareModel = JSON.parseObject("" + result, ShareListDetailModel.class);
                if (shareModel != null) {
                    //有数据
                    if (shareModel.getStatus() == 1) {
                        pageGlobal++;
                        ShareListDetailModel.DataBean.ListBean model;
                        for (ShareListDetailModel.DataBean.ListBean message : shareModel.getData().getList()) {
                            model = new ShareListDetailModel.DataBean.ListBean();
                            model.setOrder_sn(message.getOrder_sn());
                            model.setStatus_name(message.getStatus_name());
                            model.setMobile(message.getMobile());
                            model.setAdd_time(message.getAdd_time());
                            mDataList.add(model);
                        }
                        mAdapter.notifyDataSetChanged();
                    } else {
                        mPullRecyclerView.enableLoadMore(false);
                    }
                } else {
                    showErrorToast("解析出错,请重试!");
                }
            } catch (Exception ex) {
            }
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            showToast(getResources().getString(R.string.connect_sever_error_toast) );
        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {
        }

        @Override
        public void onFinished() {
            mPullRecyclerView.stopLoadMore();
        }
    };

}
